package com.glasses.manage.auth.service.impl;

import com.glasses.manage.auth.dao.AccountDao;
import com.glasses.manage.auth.dao.BECTestDao;
import com.glasses.manage.auth.entity.Account;
import com.glasses.manage.auth.service.AccountService;
import com.glasses.manage.core.biz.BaseBizImpl;
import com.glasses.manage.core.dao.BaseDao;
import com.glasses.manage.core.entity.PageBean;
import com.glasses.manage.core.entity.PageParam;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @Auther: yuyang
 * @Date: 2019/7/1 17:37
 * @ClassName:BECTestServiceImpl
 * @Description: TODO
 */
@Service("accountService")
public class AccountServiceImpl extends BaseBizImpl<Account> implements AccountService {
    private Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);

    @Autowired
    private AccountDao accountDao;

    @Override
    protected BaseDao<Account> getDao(){
        return accountDao;
    }

    @Override
    public List<Account> getOne() throws Exception  {
        //获取所有未掌握的单词
        List<Account> list=accountDao.getNAll();

        return list;
    }


    @Override
    public PageBean<Account> getAll(PageParam pageParam) {
        return accountDao.getAll(pageParam);
    }

    /**
     * 导入数据
     * @param excelIn
     * @return
     * @throws Exception
     */
    public String importExcel(InputStream excelIn) throws Exception {

        List<Account> list = new ArrayList<Account>(); //要插入的数据
        ArrayList<String> wordsList = (ArrayList) accountDao.getList();//数据库中已有的员工编号
//        logger.debug("查询数记录" + employeeNoList.size() + "条");
        try {
            Workbook rwb = Workbook.getWorkbook(excelIn);
            //定义异常列集合
            List<Cell[]> errorList = new ArrayList<Cell[]>();
            //获取sheet数据
            Sheet[] sheet = rwb.getSheets();
            //遍历sheet
            for (int i = 0; i < sheet.length; i++) {
                Sheet rs = rwb.getSheet(i);
                //遍历表格行数据
                for (int j = 1; j < rs.getRows(); j++) {
                    Cell[] cells = rs.getRow(j);
                        AccountInfo vo = setModel(cells);
                        if (vo.account == null) {
//                            logger.debug(cells[3].getContents() + ":" + vo.errorStr);
                            //记录异常数据信息
                            ArrayList<Cell> cellList = new ArrayList<Cell>();
                            cellList.addAll(Arrays.asList(cells));
                            cellList.add(new Label(cells.length, j, vo.errorStr));
                            errorList.add(cellList.toArray(new Cell[0]));
                        } else {
                            list.add(vo.account);
                        }

                }
            }
            excelIn.close();
            //保存正常数据
            accountDao.insert(list);
            //判断是否需要生成错误信息的excel
            if (errorList != null && errorList.size() > 0) {
                return "成功导入" + list.size() + "数据,其中有" + errorList.size() + "条错误信息,请下载错误信息，更正后再导入！";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "导入信息错误";
        }
        return "success";
    }

    @Override
    public Long getCount() throws Exception{
        return accountDao.getCount();
    }

    @Override
    public void update1(Account account) throws Exception {
        accountDao.update1(account);
    }



    /**
     * excel数据转换成实体类
     *
     * @param cells
     * @return
     */
    public AccountInfo setModel(Cell[] cells) {
        Account model = new Account();
        AccountInfo wordsInfo = new AccountInfo();

        String[] errStrArr = {"选择题目", "正确答案","错误答案1","错误答案2","错误答案3","解析"};

        String[] fields = {"selectTitle", "selectCorrect","selectErr1","selectErr2","selectErr3","jieXi"};

        for (int i = 0; i < 6; i++) {
            try {
                String content = StringUtils.trim(cells[i].getContents());
                if ("".equals(content)) {
                    wordsInfo.errorStr = errStrArr[i] + "不能为空";
                    return wordsInfo;
                } else {
                    if (i==0){
                        model.setSelectTitle(content);
                    }if (i==1){
                        model.setSelectCorrect(content);
                    }if (i==2){
                        model.setSelectErr1(content);
                    }if (i==3){
                        model.setSelectErr2(content);
                    }if (i==4){
                        model.setSelectErr3(content);
                    }if (i==5){
                        model.setJieXi(content);
                    }

                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                wordsInfo.errorStr = errStrArr[i] + "格式错误";
                return wordsInfo;
            }
        }

        wordsInfo.account = model;

        return wordsInfo;
    }


    private class AccountInfo {
        public Account account;
        public String errorStr;

        public AccountInfo() {
        }

        public AccountInfo(Account account, String errorStr) {
            this.account = account;
            this.errorStr = errorStr;
        }
    }

}
